package com.xckj.teacher.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xckj.teacher.R;
import com.xckj.teacher.bean.UnitBean;
import com.xckj.teacher.ui.adapter.SelectedUnitAdapter;

import java.util.List;

/**
 * @Author: ChenYao
 * @Describe: 选择单元
 * @Date: 2024/6/13 10:23
 */
public class SelectedUnitDialog extends Dialog {
    private TextView tvClose;
    private RecyclerView recUnit;
    private OnCloseClickListener closeClickListener;
    private OnItemClickListener itemClickListener;
    private List<UnitBean> unitBeans;

    public SelectedUnitDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public void setCloseClickListener(OnCloseClickListener closeClickListener) {
        this.closeClickListener = closeClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setData(List<UnitBean> unitBeans) {
        this.unitBeans = unitBeans;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_selected_unit);
        setCanceledOnTouchOutside(false);
        initView();
        initUnit();
        setListener();
    }

    private void initView() {
        tvClose = findViewById(R.id.tv_close);
        recUnit = findViewById(R.id.rec_unit);
    }

    private void initUnit() {
        recUnit.setLayoutManager(new LinearLayoutManager(recUnit.getContext()));
        SelectedUnitAdapter unitAdapter = new SelectedUnitAdapter();
        unitAdapter.setData(unitBeans);
        recUnit.setAdapter(unitAdapter);
        unitAdapter.setOnItemClickListener((unitId, versionId) -> {
            itemClickListener.onItemClick(unitId, versionId);
        });
    }

    private void setListener() {
        tvClose.setOnClickListener(view -> {
            if (null != closeClickListener) {
                closeClickListener.onCloseClick();
            }
        });
    }

    /**
     * 关闭按钮
     */
    public interface OnCloseClickListener {
        /**
         * 取消
         */
        void onCloseClick();
    }

    public interface OnItemClickListener {
        /**
         * Item的点击事件
         *
         * @param unitId    单元Id
         * @param versionId 版本Id
         */
        void onItemClick(String unitId, String versionId);
    }
}
